/**
 * 
 */
package com.sany.web.database;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author xiaoq
 * 
 */
public class LDAPConnectionFactory {

    private Connection ldapConn = null;

    private String     dbDriver = "";

    private String     dbUrl    = "";

    private String     userName = "";

    private String     userPwd  = "";

    public LDAPConnectionFactory() {
	ldapConn = null;
	OrderedProperties properties;
	try {
	    properties = OrderedProperties.load("ldap.properties");
	    dbDriver = properties.getProperty("ldap.dbDriver");
	    dbUrl = properties.getProperty("ldap.dbUrl");
	    userName = properties.getProperty("ldap.userName");
	    userPwd = properties.getProperty("ldap.userPwd");
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public void destroy() {
	ldapConn = null;
    }

    public Connection getConnection() {
	try {
	    Class.forName(dbDriver);
	ldapConn = DriverManager.getConnection(dbUrl, userName, userPwd);
	} catch (ClassNotFoundException ex) {
	    ex.printStackTrace();
	} catch (SQLException ex) {
	    ex.printStackTrace();
	}
	return ldapConn;
    }

    public void releaseConnection() {
	if (ldapConn != null) {
	    try {
		ldapConn.close();
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	}
	ldapConn = null;
    }

    /**
     * @return the dbUrl
     */
    public String getDbUrl() {
	return dbUrl;
    }

    /**
     * @param dbUrl
     *                the dbUrl to set
     */
    public void setDbUrl(String dbUrl) {
	this.dbUrl = dbUrl;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
	return userName;
    }

    /**
     * @param userName
     *                the userName to set
     */
    public void setUserName(String userName) {
	this.userName = userName;
    }

    /**
     * @return the userPwd
     */
    public String getUserPwd() {
	return userPwd;
    }

    /**
     * @param userPwd
     *                the userPwd to set
     */
    public void setUserPwd(String userPwd) {
	this.userPwd = userPwd;
    }

    /**
     * @return the dbDriver
     */
    public String getDbDriver() {
	return dbDriver;
    }

    /**
     * @param dbDriver
     *                the dbDriver to set
     */
    public void setDbDriver(String dbDriver) {
	this.dbDriver = dbDriver;
    }

}
